'use strict';

module.exports = (app) => {
  class AboutUsService extends app.Service {
    async create() {
      const { app, ctx } = this;
      const { AboutUs } = app.model.Tables.Web;
      const data = ctx.request.body;
      // 创建
      const res = await AboutUs.create({
        culture: data.culture,
        purpose: data.purpose,
        address: data.address,
        contact: data.contact,
        development: data.development,
        projectId: ctx.adminInfo.project_id,
      });

      return res;
    }

    async delete() {
      const { app, ctx } = this;
      const { AboutUs } = app.model.Tables.Web;
      const { id } = ctx.params;
      const res = await AboutUs.destroy({
        where: {
          id,
          projectId: ctx.adminInfo.project_id,
        },
      });
      if (res) {
        return '删除成功';
      }
      return 'id不存在,删除失败';
    }

    async update() {
      const { app, ctx } = this;
      const { AboutUs } = app.model.Tables.Web;
      const data = ctx.request.body;
      const res = await AboutUs.update(
        {
          culture: data.culture,
          purpose: data.purpose,
          address: data.address,
          contact: data.contact,
          development: data.development,
        },
        {
          where: {
            id: data.id,
            projectId: ctx.adminInfo.project_id,
          },
        }
      );
      if (res[0]) {
        return '修改成功';
      }
      return 'id不存在,修改失败';
    }

    async getAll() {
      const { app, ctx } = this;
      const { AboutUs } = app.model.Tables.Web;
      const data = ctx.query;

      const res = await AboutUs.findAll({
        where: {
          projectId: ctx.adminInfo.project_id,
        },
        limit: Number(data.limit) || 10,
        offset: Number(data.offset) || 0,
      });

      return res;
    }

    async getById() {
      const { app, ctx } = this;
      const { AboutUs } = app.model.Tables.Web;
      const data = ctx.query;

      const res = await AboutUs.findOne({
        where: {
          id: data.id,
        },
      });

      return res;
    }
  }
  return AboutUsService;
};
